import logging
import os
from appium import webdriver
from appium.options.common import AppiumOptions  # 需要使用这种方式传入caps参数
from base.base_page import BasePage
from pages.HomePage import HomePage


class shargeApp(BasePage):

    _sharge_apps = {
        'shargeApp_phone': 'com.sharge.glasses.phone'
    }

    _app_activity = '.MainActivity'
    _device_name = 'f76ecbf0'
    _platformName = 'android'
    _platformVersion = '11.0'

    """
    _udid和_port是多设备参数，用于命令行中测试多台设备
    命令行中输入udid="设备的udid" port=4723 pytest test_xxx.py
    命令行中输入udid="设备的udid" port=4725 pytest test_xxx.py
    注意端口号要隔1个数，例如第一台设备使用的的端口号是4723，第二台设备使用的端口号就用4725
    """
    _udid = os.getenv("udid")
    _port = os.getenv("port")

    """
    初始化app
    进行安装待测app、安装appium服务app、授权、隐式等待等等配置
    """
    def init(self):
        logging.info("初始化app")
        # 判断是否有传入driver，没有就新增，有就复用
        if self.driver is None:
            logging.info(f"driver为'{self.driver}'")
            # 初始化app所需caps
            options = AppiumOptions()
            options.load_capabilities({
                "platformName": self._platformName,
                "appium:automationName": "UiAutomator2",
                "appium:deviceName": self._device_name,
                "appium:appPackage": self._sharge_apps['shargeApp_phone'],
                "appium:appActivity": self._app_activity,
                "appium:platformVersion": self._platformVersion,
                "appium:ensureWebviewsHavePages": True,
                "appium:nativeWebScreenshot": True,
                "appium:newCommandTimeout": 3600,
                "appium:connectHardwareKeyboard": True
            })

            # 使用caps参数连接appium server
            self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", options=options)
            self.driver.implicitly_wait(5)
            self.driver.update_settings({"waitForIdleTimeout": 0})
            # 如果要测试多台设备则需要通过命令行传入的port和udid进行分配
            # self.driver = webdriver.Remote(f"http://127.0.0.1:{self._port}/wd/hub", caps)
            logging.info(f"新建driver为'{self.driver}'")
        else:
            logging.info(f"复用driver为'{self.driver}'")
        # return HomePage(self.driver)

    # 启动app，根据实际测试情况决定是否清除app数据和设置
    def start(self):
        logging.info("初始化完成，启动app")
        if self.driver is None:
            logging.info(f"driver为'{self.driver}'")

            # 初始化app所需caps
            options = AppiumOptions()
            options.load_capabilities({
                "platformName": self._platformName,
                "appium:automationName": "UiAutomator2",
                "appium:deviceName": self._device_name,
                "appium:appPackage": self._sharge_apps['shargeApp_phone'],
                "appium:appActivity": self._app_activity,
                "appium:platformVersion": self._platformVersion,
                "appium:ensureWebviewsHavePages": True,
                "appium:nativeWebScreenshot": True,
                "appium:newCommandTimeout": 3600,
                "appium:connectHardwareKeyboard": True
            })

            # 使用caps参数连接appium server
            self.driver = webdriver.Remote("http://127.0.0.1:4723", options=options)
            self.driver.implicitly_wait(5)

        else:
            logging.info(f"复用driver为'{self.driver}'")
            self.open_launcher_app()
        return self.driver


    def open_launcher_app(self):
        self.driver.activate_app(self._sharge_apps['shargeApp_phone'])